{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import QUANTAXIS as QA"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "\"\"\"\n",
    "在讨论完QA_fetch_get系列以后 我们主要考虑的是从本地获取数据并进行分析\n",
    "\n",
    "QA_fetch_xxx_adv系列\n",
    "\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 首先进行数据获取 多个股票的日线数据\n",
    "\n",
    "#QA.QA_fetch_stock_day_adv(\"000001\",'2017-01-01','2017-11-30')\n",
    "data=QA.QA_fetch_stock_day_adv([\"000001\",'000002','000004'],'2017-01-01','2017-11-30')\n",
    "\n",
    "print(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 一些基础的值\n",
    "\n",
    "data.open\n",
    "data.close\n",
    "data.high\n",
    "data.low\n",
    "data.date\n",
    "data.datetime\n",
    "data.code"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 对数据进行复权处理\n",
    "\n",
    "#data=data.to_hfq() # 批量后复权\n",
    "data=data.to_qfq() # 批量前复权"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 查看数据\n",
    "# 有三种方式查看数据\n",
    "\n",
    "data.show()\n",
    "data()\n",
    "data.data\n",
    "# 一般我们推荐直接用()的方式查看数据 这个数据是Pandas.Dataframe格式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 数据翻转 .reverse()\n",
    "reversed_data=data.reverse()\n",
    "print(reversed_data)\n",
    "print(reverse_data())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 数据查询 .query()\n",
    "data.query('date==\"2017-01-03\"')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 数据筛选- 选取某一只股票的数据 select_code\n",
    "data.select_code('000001')\n",
    "print(data.select_code('000001')())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 数据筛选- 选取某一段时间内的数据\n",
    "res=data.select_time('2017-01-01','2017-01-31')\n",
    "print(res.data)\n",
    "# 如果end设置为空,则获取到从start开始至今的所有数据\n",
    "res=data.select_time('2017-11-01')\n",
    "print(res.data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 数据筛选 - 选取某一个时间点向前向后的数据\n",
    "# method\n",
    "# lt 小于 <\n",
    "# lte 小于等于  <=\n",
    "# gt 大于 >\n",
    "# gte 大于等于 >=\n",
    "# e 等于 ==\n",
    "res=data.select_time_with_gap(gap=5,method='lte',time='2017-01-31')\n",
    "print(res.data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 数据筛选 - 拿到某一天的股票数据\n",
    "# if_trade的作用是  如果if_trade=False 则遇到停牌时 会返回最后一个交易日的bar\n",
    "res=data.get_bar(code='000001',time='2017-01-20',if_trade=True)\n",
    "print(res.data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 数据切面 pivot\n",
    "\n",
    "res=data.pivot('open')\n",
    "print(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 对数据块应用函数 add_func\n",
    "# 一般是配合指标函数去计算指标\n",
    "\n",
    "# QA.QA_indicator_ATR  ATR指标\n",
    "\n",
    "res=data.add_func(QA.QA_indicator_ATR,10)\n",
    "print(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 格式转换 to_dict, to_list, to_numpy\n",
    "res=data.to_dict()\n",
    "print(res)\n",
    "res=data.to_list()\n",
    "print(res)\n",
    "res=data.to_numpy()\n",
    "print(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 画图\n",
    "\n",
    "data.plot('000001') #画某一只股票\n",
    "data.plot()  #画全部股票"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
